#!/bin/bash
[ -f /usr/bin/tac ] && tac="/usr/bin/tac" || tac="tail -r"
UASTR="Stanza/3.0 iPhone OS/5.0/iPhone catalog/3.0"

shucang() {
  echo $@
  curl -f -v -L -A Stanza -C - "http://www.shucang.org/s/bookService.php?vk=$1" -o "sc/$1.epub"
  [ $? -eq 0 ] && echo "$@" >> sc.txt || echo "$@" >> sc.err
}

newsc() {
  uid=${1:-5976}
  last=`head -n 1 $uid.txt|cut -d' ' -f 1`
  PAGE=`curl -s -L -A iPhone "http://www.shucang.org/m/members.php?UID=$uid"|grep -m 1 -B 999 $last`
  [ -z "$PAGE" ] && { echo "Cannot find entry. Check more page"; return 1; }
  books=`echo "$PAGE"|grep btitle|cut -d= -f4|cut -d'<' -f1|tr '">' ' '`
  [ -z "$books" ] && { echo No new books; return 0; }
  echo "$books"
  echo "Start downloading:"
  echo "$books"|$tac|while read bid btitle;do
    echo $bid $btitle
    curl -f -# -L -A Stanza -C - "http://www.shucang.org/s/bookService.php?vk=$bid" -o "sc/$bid.epub"
    [ $? -eq 0 ] && sed -i "" -e "1i\\
    $bid  $btitle" $uid.txt || echo "$bid $btitle" >> sc.err
  done
}

zipepub() {
  cd $1
  fname=${1%/*}
  fname=${fname##*/}.epub
  echo Creating $fname from $1
  XMLstr='<?xml version="1.0" encoding="UTF-8" ?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
   <rootfiles>
      <rootfile full-path="OPS/fb.opf" media-type="application/oebps-package+xml"/>
   </rootfiles>
</container>'
  [ -f mimetype ] || echo application/epub+zip > mimetype
  [ -f META-INF/container.xml ] || mkdir META-INF && echo "$XMLstr" > META-INF/container.xml
  zip -X0 $fname mimetype
  zip -rDXq $fname * -x "*.DS_Store" -x mimetype
  mv $fname ..
  cd -
}

cnepub0() {
  echo $@
  btitle=`curl -s "http://www.cnepub.com/$1.html"|grep -o "<h1>[^<]*"|cut -d'>' -f2|tr ' ' '_'`
  btitle2=`python -c "import urllib; print urllib.quote('''$btitle''')"`
  bid=`echo $1|grep -o "[0-9]\+"`
  bid1=$((bid/10000))
  [ $bid1 -lt 10 ] && bid1="0$bid1"
  curl -f -# -C - -A Stanza "http://www.cnepub.com/bookfiles/000/$bid1/${bid: -4:2}/${bid: -2:2}/$btitle2.epub" -o "ce/$bid.epub"
  [ $? -eq 0 ] && echo "$@" >> ce.txt || echo "$@" >> ce.err
}

cnepub() {
  echo $@
  curl -f -# -L -A "$UASTR" -u l11:fit4305 -C - "http://www.cnepub.com/online_catalog/user_authorize.php?id=$1" -o "ce/$1.epub"
  [ $? -eq 0 ] && echo "$@" >> ce.txt || echo "$@" >> ce.err
}

cnepub2() {
  # download html and jpg files individually
  echo $@
  curl="curl -A iPad --create-dirs -C -"
  bid=`echo $1|grep -o "[0-9]\+"`
  bid1=$((bid/10000))
  [ $bid1 -lt 10 ] && bid1="0$bid1"
  ROOT="http://www.cnepub.com/bookfiles/000/$bid1/${bid: -4:2}/${bid: -2:2}/$bid/OPS"
  froot="$bid/OPS"
  $curl -s -f "$ROOT/fb.opf" -o "$froot/fb.opf"
  [ $? -eq 0 ] || echo $@ >> ce.err && return 1
  pages=`grep -o 'href="[^"]*' "$froot/fb.opf"|cut -d'"' -f2`
  pages=`echo $pages|tr ' ' ','`
  $curl -s "$ROOT/{$pages}" -o "$froot/#1"
  imgs=`grep -h -o 'src="images[^"]*' $froot/*.html|cut -d'/' -f2`
  imgs=`echo $imgs|tr ' ' ','`
  $curl -# "$ROOT/images/{$imgs}" -o "$froot/images/#1"
  zipepub $bid
}

cefilter() {
  grep -o 'class="pl2"><[^<]*' | cut -d'/' -f2 | sed -e 's/\.html"  target="_blank".*>/ /'
}

newce() {
  case "$1" in
    c)
    f=catalog_id
    ;;
    p)
    f=publisher_uid
    ;;
    *)
    f=uploadby_uid
    ;;
  esac
  fv=${2:-49396}
  last=`head -n 1 "$fv.txt"|cut -d' ' -f 1`
  pn=${3:-1}
  URL="http://www.cnepub.com/search/?f=$f&fv=$fv&page=$pn"
  echo "$URL"

  books=`curl -s -L -A iPad "$URL"| cefilter`
  newbooks=`echo "$books" | grep -m 1 -B 99 $last`
  if [ -z "$newbooks" ]; then
    echo "$books"
    echo -n "Cannot find entry. Download all books? (Y/n):"
    read -n 1 -t 5 ifdld
    if [ "$ifdld" == "n" ]; then
      echo " Try page $((pn+1))"
      return 0;
    else
      newbooks=`echo "$books"|$tac`
    fi
  else
    newbooks=`echo "$newbooks"|$tac|sed 1d`
    [ -z "$newbooks" ] && { echo "No new books"; return 0; }
  fi

  echo "$newbooks"
  echo "Start downloading:"
  echo "$newbooks"|while read bid btitle;do
    echo $bid $btitle
    curl -f -# -L -C - -A "$UASTR" -u l11:fit4305 "http://www.cnepub.com/online_catalog/user_authorize.php?id=$bid" -o "ce/$bid.epub"
    [ $? -eq 0 ] && sed -i "" -e "1i\\
    $bid $btitle" $fv.txt || echo "$bid $btitle" >> ce.err
  done
}
